{{!connect "widget/top_menu_settings.js"}}

<ul class="top_menu service-menu">
	{{top_menu_content|safe}}
	{{?user_role=="administrator"}}
	<li class="item- edit- top_menu_edit"><a href="javascript: startEditTopMenu();">Редактировать</a></li>
	{{/?}}
</ul>


{{?user_role=="administrator"}}
<style>
.top_menu_edit { display: none; }
.moderator .top_menu_edit { display: block; }
.content- input { width: 400px; }
.top_menu_item { padding-bottom: 24px; }
.edit_top_menu_content { height: 400px; }
</style>

<a href="{{/}}get_top_menu_settings" id="top_menu_settiings_action"></a>

<div class="popup-widgets" id="top_menu_edit_popup" style="display: none;">
	<form action="{{/}}save_top_menu" method="post" id="save_top_menu_form">
	<h2 class="title-">Редактирование глобального меню</h2>
	<div class="content- edit_top_menu_content">
    <a href="javascript: modeSort();" id="sort_top_menu_trigger">сортировать пункты меню</a>
    <a href="javascript: modeEdit();" id="edit_top_menu_trigger" style="display: none;">редактировать пункты меню</a>
    <br /><br />
    <div id="top_menu_settings" class="wg-droppable"></div>
  </div>
	<div class="buttons-">
		<a href="javascript: saveEditTopMenu();" class="bn"><img src="{{images}}button-save-gray.gif" alt"Закрыть" class="submit- button-" /></a>
		<a href="javascript: closeEditTopMenu();" class="bn"><img src="{{images}}button-cancel-gray.gif" alt"Закрыть" class="cancel- button-" /></a>
	</div>
	</form>
</div>

<!-- script>
  function modeSort() {
    dctrl.modOn();
    jQuery("#edit_top_menu_trigger").show();
    jQuery("#sort_top_menu_trigger").hide();
  }
  
  function modeEdit() {
    dctrl.modOff();
    jQuery("#edit_top_menu_trigger").hide();
    jQuery("#sort_top_menu_trigger").show();
  }
  
	function startEditTopMenu() {
		var div = jQuery("#top_menu_edit_popup");
		
		if ( div.get(0) ) {
			jQuery.get("{{/}}get_top_menu_settings", function(data) {
				jQuery("#top_menu_settings").html(data);
        
				div.show();
			});
		}
	}
	
	function closeEditTopMenu() {
		var div = jQuery("#top_menu_edit_popup");
		
		if ( div.get(0) ) {
			div.hide();
		}
	}
	function saveEditTopMenu() {
		var div = jQuery("#top_menu_edit_popup");
		
		if ( !div.get(0) ) {
			return;
		}
		
    var position = new Array();
		div.find("div.js_linkdiv").each( function(i) {
      position.push(this.id);
		});
    
    position_string = position.join(",");
		
		var itemsString = jQuery("#save_top_menu_form").serialize();
		var action = jQuery("#save_top_menu_form").get(0).action;
		
		jQuery.get(action, { "items": itemsString, "position": position_string }, function(data) {
			if ( "error" == data ) {
				alert("Ошибка при сохранении формы.");
				return;
			}
			
			jQuery("#top_menu_content").html(data);
			
			closeEditTopMenu();
		});
	}
</script -->
{{/?}}